﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace ShopOnline
{
    public partial class Contact : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((ProductMaster)this.Master).SetPageTitle("Contact");

            if (!IsPostBack)
            {
                BannerKey = ShopOnline.BLL.BannerKey.Contact;
                if (View_Customers.GetCurrentUser() != null)
                {
                    txtName.Text = View_Customers.GetCurrentUser().s_CustomerName;
                    txtAddress.Text = View_Customers.GetCurrentUser().s_Address;
                    txtEmail.Text = View_Customers.GetCurrentUser().s_Email;
                }
            }
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtCode.Text);
            if (!ccJoin.UserValidated)
            {
                txtCode.Text = "";
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'Invaid validation code! Please type again.', 8000);", 1), true);
                return;
            }
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                CustomerRequests req = CustomerRequests.AddNewRequest(txtName.Text, txtEmail.Text, txtAddress.Text, txtSubject.Text, txtContent.Text.Replace("\n", "<br/>"));
                AttachedFiles file = null;
                string dir = POSSystem.BLL.Utilities.GetConfigAppSetting("AttachedFilePath");
                if (!System.IO.Directory.Exists(Server.MapPath(dir)))
                    System.IO.Directory.CreateDirectory(Server.MapPath(dir));

                string fileName;
                string filePath;

                if (file1.HasFile || file2.HasFile)
                {
                    dir += "/" + Guid.NewGuid().ToString();
                    System.IO.Directory.CreateDirectory(Server.MapPath(dir));
                }

                if (file1.HasFile)
                {
                    if (file == null)
                        file = new AttachedFiles();
                    fileName = System.IO.Path.GetFileName(file1.PostedFile.FileName);
                    filePath = dir + "/" + fileName;
                    file.AttachFile(req.ID, AttachedFiles.FileCategory.CustomerRequest, fileName, filePath);
                    file1.SaveAs(Server.MapPath(filePath));
                }

                if (file2.HasFile)
                {
                    if (file == null)
                        file = new AttachedFiles();
                    fileName = System.IO.Path.GetFileName(file2.PostedFile.FileName);
                    filePath = dir + "/" + fileName;
                    file.AttachFile(req.ID, AttachedFiles.FileCategory.CustomerRequest, fileName, filePath);
                    file2.SaveAs(Server.MapPath(filePath));
                }

                if (file != null)
                    file.Save();

                tx.CommitTransaction();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your information has been sent. Thank you for contacting us!', 8000);", 1), true);
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("Contact.aspx.cs", "btnSubmit_OnClick", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'There were a error in sending your request. Please contact administrator. Thank you!', 8000);", 1), true);
            }
        }
    }
}